#include "Parser.h"
#include <math.h>
#include <iostream>
#include "Error.h"

using namespace fileIO;
using namespace std;







// *********************************************************************
// PARSER
// *********************************************************************


Parser::Parser(string nom,int mode) : lex(nom,mode) {
	_filename=nom;
}

void Parser::analyse(int debut,int fin) {
  while (!lex.isEnd()) {
    lex.skipToken();
    if (((lex.nbLigne()>=debut) || (debut==0)) && (fin==0 || (lex.nbLigne()<=fin)))
      lex.check();
  }
  cout << "nb_ligne : " << lex.nbLigne() << endl;
  exit(1);
}

Parser::~Parser() {
//  cout << _filename << " done" << endl;
//	cout << "*******************************\n";
}

int Parser::readInteger() {
	int res=0;
	if ((lexer().token()==CARDINAL) || (lexer().token()==INTEGER)) {
		res=lexer().entier();
	}
	else error("entier attendu",__LINE__,__FILE__);
	skipToken();
	return res;
}

double Parser::readDouble() {
	double res=0.0;
	if (lexer().token()==DOUBLE) {
		res=lexer().reel();
	}
	else error("reel attendu =>"+sToken(),__LINE__,__FILE__);
	skipToken();
	return res;
}

double Parser::readNumber() {
  double res=0.0;
	if ((lexer().token()==CARDINAL) || (lexer().token()==INTEGER)) {
		res=float(lexer().entier());
	}
	else if (lexer().token()==DOUBLE) {
	  res=lexer().reel();
	}
	else error("nombre (reel ou entier) attendu =>"+sToken(),__LINE__,__FILE__);
	skipToken();
	return res;
}

void Parser::read(const string &s) {
  if (sToken()!=s) error("waiting : "+s+", got : "+sToken(),__LINE__,__FILE__);
  skipToken();
}

bool Parser::readString(const string &s) {
  return lex.readString(s);
}

int Parser::readNextInteger() {
  while ((token()!=INTEGER) & (token()!=CARDINAL)) {
    skipToken();
  }
  int res=lex.entier();
  skipToken();
  return res;
}

/*
string lexer::lireMot() {
  while ((!le_car==' ' ) && (!le_car==char(10)) && (!le_car==char(13))) {
    la_chaine+=le_car;
    skipCar();
  }
}
*/

void Parser::close() {
  lex.close();
}

void Parser::skipToken() {
	lex.skipToken();
  while (!lex.isEnd() && (token()==LINE_COMMENT)) {
    lex.skipToken();
  }
//	lex.check();
//	cout << "sToken : " << sToken() << endl;
}

void Parser::skipMot() {
  lex.skipMot();
}

void Parser::skipMotSameLine() {
  lex.skipMotSameLine();
}

bool Parser::isOk() {
  return lex.isOk();
}

bool Parser::isOpen() {
  return lex.isOpen();
}


unsigned int Parser::cToken() {
  return lex.cardinal();
}

int Parser::iToken() {
  return lex.entier();
}

double Parser::dToken() {
  return lex.reel();
}

bool Parser::isNumber() {
  return ((token()==DOUBLE) || (token()==INTEGER) || (token()==CARDINAL));
}

void Parser::setCommentLine(string s) {
  lex.setCommentLine(s);
}

void Parser::setSeparator(string s) {
  lex.setSeparator(s);
}

string Parser::filename() {
  return lex.filename();
}


// ***********************************************************************************

